package leetcode4

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	count := len(nums1) + len(nums2)
	var (
		flag  bool
		mid   = 0
		res   = make([]int, 2)
		index = 0
		i     = 0
		j     = 0
	)
	if count%2 == 1 {
		flag = true
	}
	mid = count / 2

	for {

		if flag {
			if index == mid+1 {
				return float64(res[1])
			}
		} else {
			if index == mid+1 {
				return float64(res[0]+res[1]) / 2
			}
		}
		if i > len(nums1)-1 {
			res[0], res[1] = res[1], nums2[j]
			j++
		} else if j > len(nums2)-1 {
			res[0], res[1] = res[1], nums1[i]
			i++
		} else {
			if nums1[i] > nums2[j] {
				res[0], res[1] = res[1], nums2[j]
				j++
			} else {
				res[0], res[1] = res[1], nums1[i]
				i++
			}
		}
		index++
	}
}
